package com.example.commoncore.ext;

import com.google.common.escape.Escaper;
import com.google.common.html.HtmlEscapers;

import java.beans.PropertyEditorSupport;

/**
 * json字符串转换
 */
public class StringEscapeEditor extends PropertyEditorSupport {
    public StringEscapeEditor() {}

    @Override
    public String getAsText() {
        Object value = getValue();
        return value != null ? value.toString() : "";
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (text == null) {
            setValue(null);
        } else {
            Escaper htmlEscaper =HtmlEscapers.htmlEscaper();
            setValue(htmlEscaper.escape(text));
        }
    }

}
